package com.sgb.kjwl.view.ui.fragment.pick;

import androidx.databinding.DataBindingUtil;
import android.os.Bundle;
import androidx.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.sgb.kjwl.AppFragment;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.FragmentPickDetailList3Binding;
import com.sgb.kjwl.model.entity.pick.TenderDictEntity3;
import com.sgb.kjwl.model.entity.pick.TenderInfoListEntity3;
import com.swgk.core.event.EventConstant;
import com.sgb.kjwl.view.event.PickEvent;
import com.sgb.kjwl.view.widget.pick.PickDetailListView;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.List;

/**
 * 正在招标详情页面 V2.5
 */
public class PickDetailListFragment3 extends AppFragment {

    private FragmentPickDetailList3Binding binding;

    public static PickDetailListFragment3 newInstance() {
        PickDetailListFragment3 fragment = new PickDetailListFragment3();
        Bundle bundle = new Bundle();
        fragment.setArguments(bundle);
        return fragment;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        binding = DataBindingUtil.inflate(inflater, R.layout.fragment_pick_detail_list3, container, false);
        return binding.getRoot();
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this);
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN, sticky = true)
    public void onMessageEvent(PickEvent event) {
        if (event.getMessageTag() == EventConstant.EVENT_PICK_DETAIL_LIST) {
            TenderInfoListEntity3 tenderInfoListEntity3 = (TenderInfoListEntity3) event.getMessageData();
            showInfoView(tenderInfoListEntity3);
        }
    }

    private void showInfoView(TenderInfoListEntity3 tenderInfoListEntity3) {
        if (tenderInfoListEntity3 == null) {
//            binding.emptyView.setVisibility(View.VISIBLE);
            return;
        }
        List<List<TenderDictEntity3>> tenderList = tenderInfoListEntity3.getTenderInfoItemEntity3();
        if (tenderList == null) {
//            binding.emptyView.setVisibility(View.VISIBLE);
            return;
        }
        if (tenderList.size() == 0) {
//            binding.emptyView.setVisibility(View.VISIBLE);
            return;
        }
        binding.rootLayout.removeAllViews();
        for (List<TenderDictEntity3> tenderDictEntity : tenderList) {
            addItemView(tenderDictEntity);
        }
    }

    private void addItemView(List<TenderDictEntity3> tenderDictEntity) {
        PickDetailListView pickDetailListView = new PickDetailListView(getActivity());
        pickDetailListView.setRecyclerData(tenderDictEntity);
        binding.rootLayout.addView(pickDetailListView);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}
